﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using jiaju.Areas.Back.Models;
using jiaju.Areas.Back.Models.DBModel;
using jiaju.Areas.Back.Models.DAL;

namespace jiaju.Areas.Back.Controllers
{
    [Authorize]
    public class PicutreController : Controller
    {
        // GET: Back/Picutre
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult getPicutre(int page, int limit)
        {
            //调用Service里的方法
            List<Picutre>  dataList = PicutreService.getPicutreList(page, limit);
            //获取总条数
            string sql = "select COUNT(*) from dbo.picture";
            int cout = (int)DBHelp.ExcuteScalar(sql);
            Object obj = new
            {
                code = 0,
                msg = "请求成功",
                count = cout,
                data = dataList
            };
            return Json(obj, JsonRequestBehavior.AllowGet);
        }
        //添加商品图片列表页面   
        public ActionResult Add()
        {
            List<ProductType> titleList = ProductTypeService.getAllName();
            ViewBag.A = titleList;
            List<Product> List = ProductService.getAllZuZhuang();
            ViewBag.B = List;
            return View();
        }
        public ActionResult AddDel(Picutre art)
        {
            //新增 请求数据库
            int n = PicutreService.AddPicutre(art);
            if (n > 0)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }
        }
        //编辑商品图片列表
        public ActionResult Edit(string Id)
        {
            //通过Id获取数据详情
            Picutre art = PicutreService.GetById(Id);
            //把详情数据传到页面
            ViewBag.A = art;          
            return View();
        }
        [HttpPost]
        public ActionResult EditDel(Picutre art)
        {
            //编辑，请求数据库
            int m = PicutreService.EditPicutre(art);
            if (m > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败" });
            }
        }
        //删除商品图片方法DelArt
        public ActionResult DelArt(String Id)
        {
            int m = PicutreService.DelPicutre(Id);
            if (m > 0)
            {
                return Json(new { success = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" });
            }
        }

        //处理图片上传UploadPhoto
        [HttpPost] //post 请求
        public ActionResult UploadPhoto()
        {
            //1.获取上传图片
            HttpPostedFileBase postFile = Request.Files["uplaodImg"];
            if (postFile != null)
            {
                //2.图片传过来，改名字 年与日时分秒毫秒.png
                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                //3.图片存到哪                  
                string fullUpLoadPath = Server.MapPath("~/images/");
                //4.保存图片到路径+名称
                postFile.SaveAs(fullUpLoadPath + newFileName);
                //返回地址
                return Json(new { success = true, path = "images/" + newFileName, msg = "上传成功" });
            }
            else
            {
                return Json(new { success = false, path = "", msg = "上传失败" });
            }
        }
    }
}